package com.zhsm.fuxi.mvp.ui.activitys

import android.content.Intent
import android.graphics.Bitmap
import android.view.View
import com.hankkin.reading.base.BaseMvpActivity
import com.zhsm.paipaiout.baselibrary.utils.ToastUtils
import com.zhsm.fuxi.R
import com.zhsm.fuxi.mvp.contract.LoginContract
import com.zhsm.fuxi.mvp.presenter.LoginPresenter
import com.zhsm.fuxi.mvp.ui.dialog.VerificationCodeDialog
import com.zhsm.fuxi.utils.CommonUtils
import com.zhsm.paipaiout.baselibrary.widget.view.MyTextWatcher
import kotlinx.android.synthetic.main.activity_login.*

/**
 * Created by GuZhC on 2018/10/19.
 * describe:
 */
class LoginActivity : BaseMvpActivity<LoginPresenter>(), LoginContract.IView, View.OnClickListener {


    private lateinit var mCodeDialog: VerificationCodeDialog
    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun registerPresenter() = LoginPresenter::class.java
    override fun isSupportSwipeBack(): Boolean {
        return false
    }

    override fun initView() {
        setMiuiStatusBar()
        setClickListener()
        inputNum.addTextChangedListener(object : MyTextWatcher() {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (s?.length!! > 0) deleteInputNum.visibility = View.VISIBLE
                else deleteInputNum.visibility = View.GONE
                setLoginEnable()
            }
        })
        inputPsw.addTextChangedListener(object : MyTextWatcher() {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                setLoginEnable()
            }
        })
        mCodeDialog = VerificationCodeDialog(this)
        mCodeDialog.setVerificationCodeLisenter(object : VerificationCodeDialog.VerificationCodeDialogImp {
            override fun refreshVerification() {
                var phone = inputNum.text.toString()
                if (CommonUtils.checkPhoneNum(phone)) getPresenter().sendImgCode(phone)
                else ToastUtils.makeText(resources.getString(R.string.num_error))
            }

            override fun sendVerificationCode(code: String) {
                mCodeDialog.cancel()
                // 处理发送验证码
                getPresenter().sendSmsCode(inputNum.text.toString(), code)
            }
        })
    }

    private fun setClickListener() {
        deleteInputNum.setOnClickListener(this)
        tvProtocol.setOnClickListener(this)
        getCodeHint.setOnClickListener(this)
        protocol.setOnClickListener(this)
        Login.setOnClickListener(this)
        cbAgreement.setOnClickListener(this)
    }

    override fun setImageCodeUrl(bitmap: Bitmap?) {
        if (bitmap != null) {
            mCodeDialog.showImageCode(bitmap)
        }
    }

    override fun setCountDownText(s: Int) {
        if (getCodeHint.isClickable) getCodeHint.isClickable = false
        getCodeHint.text = "$s 秒后重新获取"
        if (s == 0) {
            getCodeHint.text = getString(R.string.get_verif_code)
            getCodeHint.isClickable = true
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.deleteInputNum -> inputNum.setText("")
            R.id.tvProtocol -> ToastUtils.makeText("look 协议")
            R.id.getCodeHint -> {
                val phone = inputNum.text.toString()
                if (CommonUtils.checkPhoneNum(phone)) getPresenter().sendImgCode(phone)
                else ToastUtils.makeText(resources.getString(R.string.num_error))
            }
            R.id.protocol, R.id.cbAgreement -> {
                cbAgreement.isChecked = !cbAgreement.isChecked
                setLoginEnable()
            }
            R.id.Login -> {
                getPresenter().Login(inputNum.text.toString(), inputPsw.text.toString())
            }
        }
    }

    private fun setLoginEnable() {
        val phone = inputNum.text.toString()
        val code = inputPsw.text.toString()
        Login.isEnabled = !phone.isNullOrEmpty() && !code.isNullOrEmpty() && cbAgreement.isChecked
    }


    override fun loginSucces() {
        hideLoding()
        startActivity(Intent(this, MainActivity::class.java))
        this.finish()
    }
}